
#include "Test.h"

#if TEST_SpiFlash

#include <string.h>
#include <stdint.h>

#include "W25Q.h"
#include "Debug.h"

static void TestSpiFlash_Present(void){
	if(!W25Q_Present()){
		DBG_PRINT("W25Q not present\r\n");
	}
	while(!W25Q_Present()){
	}
}

static void TestSpiFlash_ReadWrite(void){
	uint8_t read_buf[16];
	uint8_t write_buf[16];
	uint32_t i;
	
	W25Q_Read(256, sizeof(read_buf), read_buf);
	DBG_WRITE("Read:"); DBG_PRINT_BUF(read_buf, sizeof(read_buf));DBG_WRITE("\r\n");
	for(i = 0; i < sizeof(write_buf); i++){
		write_buf[i] = read_buf[i] + 1;
	}
	W25Q_ErasaMemory(0, 4096);
	W25Q_WriteMemory(256, sizeof(write_buf), write_buf);
	DBG_WRITE("Write:"); DBG_PRINT_BUF(write_buf, sizeof(write_buf));DBG_WRITE("\r\n");
	W25Q_Read(256, sizeof(read_buf), read_buf);
	DBG_WRITE("Read:"); DBG_PRINT_BUF(read_buf, sizeof(read_buf));DBG_WRITE("\r\n");
	if(memcmp(read_buf, write_buf, sizeof(write_buf))){
		DBG_WRITE("FAIL!\r\n\r\n");
		while(1);
	}
}
#endif 

void TestSpiFlash(void){
#if TEST_SpiFlash
	DBG_PRINT("%s\r\n", __func__);
//		TestSpiFlash_Present();
	TestSpiFlash_ReadWrite();
	DBG_PRINT("\r\n");
#if 0
	while(1){
		uint8_t read_buf[4];
		W25Q_Read(256, sizeof(read_buf), read_buf);
	}
#endif 
#endif 
}


